<?php
namespace App\Http\Controllers\Supplier;

use App\Models\Category;
use App\Models\CategoryUser;
use App\Models\GoodsRate;
use App\Services\CommonService;
use Illuminate\Http\Request;

//品类管理
class CategoryManagementController extends BaseController
{
    //品类管理列表
    public function categoryUserList(Request $request)
    {
        $input = $request->input();
        $where = array('cu.member_id' => $this->shopUser['member_id'],
            'cu.status' => CategoryUser::CATEGORY_USER_STATUS_ENABLE,
            'cu.is_delete' => CategoryUser::CATEGORY_USER_NOT_DELETE,
            'c.enabled' => Category::CATEGORY_STATUS_DISPLAY,
            'c.is_delete' => Category::CATEGORY_NOT_DELETE
        );
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $data = CategoryUser::GetCategoryManagementList($pageSize, $where);
        $data = formatPage($data);
        $info = $data['data'];
        foreach ($info as $k => $v) {
            $info[$k]['create_time'] = !empty($v['create_time']) ? dateFormat($v['create_time']) : '';
            $parent_category = Category::select('id', 'name', 'image')
                ->where(array('id' => $v['parentid']))
                ->first();
            $parent_category = $parent_category ? $parent_category->toArray() : [];
            $info[$k]['category_name'] = $parent_category['name'] . '->' . $v['name'];
            $goodsRate = GoodsRate::GetGoodsRate(array('gr.member_id' => $v['member_id'], 'gr.category_id' => $v['category_id'], 'gr.is_delete' => GoodsRate::GOODS_RATE_NOT_DELETE)); //商品费率
            $category_rate = '';
            foreach ($goodsRate as $key => $val) {
                $category_rate .= $val['source_name'] . '(' . $val['fixed_cost'] . '元 + ' . $val['goods_rate'] . '%),';
            }
            $info[$k]['category_rate'] = rtrim($category_rate, ',');
            if ($v['ischeck'] == CategoryUser::CATEGORY_USER_REFUSE_CHECK) {
                //审核拒绝原因
                if ($v['check_refuse_type'] == 1) { //审核拒绝类型：1营业执照经营范围不包含该品类 2其他原因
                    $info[$k]['reason'] = '营业执照经营范围不包含该品类';
                } else {
                    $info[$k]['reason'] = $v['check_refuse_reason'];
                }
            } else {
                $info[$k]['reason'] = '';
            }
        }
        $data['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //根据id编辑商户分类信息的审核状态(重新提交)
    public function editCategoryUserIscheck(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '商户分类id为空');
        }
        if (empty($input['ischeck'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '审核状态为空');
        }
        $categoryUser = CategoryUser::GetCategoryUserById($input['id']);
        if ($categoryUser['ischeck'] == CategoryUser::CATEGORY_USER_REFUSE_CHECK) { //审核状态：1已审核 2待审核 3拒绝审核
            CategoryUser::editDataById($input['id'], array('ischeck' => $input['ischeck'], 'update_time' => date('Y-m-d H:i:s')));
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '编辑成功');
        } else {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '当前审核状态不是拒绝审核');
        }
    }

    //根据id删除商户分类信息
    public function delCategoryUser(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '商户分类id为空');
        }
        $categoryUser = CategoryUser::GetCategoryUserById($input['id']);
        if ($categoryUser['ischeck'] == CategoryUser::CATEGORY_USER_REFUSE_CHECK) { //审核状态：1已审核 2待审核 3拒绝审核
            CategoryUser::editDataById($input['id'], array('is_delete' => CategoryUser::CATEGORY_USER_IS_DELETE, 'update_time' => date('Y-m-d H:i:s')));
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '删除成功');
        } else {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '当前审核状态不是拒绝审核');
        }

    }

    //获取该商户的品类信息
    public function categoryInfoByMemberid()
    {
        $other_admin = $this->users; //用户信息
        $applyCategory = CategoryUser::GetApplyCategoryByMemberid($other_admin['user_id']); //已申请品类
        //未申请品类
        $parentCategory = Category::GetParentCate();
        foreach ($parentCategory as $k => $v) {
            $parentCategory[$k]['children'] = $this->getChild($v['id'], $other_admin['user_id']);
        }
        $data = array('applyCategory' => $applyCategory, 'notApplyCategory' => $parentCategory);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //获取父级分类下的未申请子级
    private function getChild($pid, $member_id)
    {
        $scecondCategoryByMemberid = CategoryUser::GetSecondCategoryByMemberid($pid, $member_id);
        $scecondCategory = Category::GetSonCate($pid);
        foreach ($scecondCategoryByMemberid as $kk => $vv) {
            foreach ($scecondCategory as $key => $value) {
                if ($vv['id'] == $value['id']) {
                    unset($scecondCategory[$key]);
                }
            }
        }
        return array_values($scecondCategory);
    }

    //申请品类
    public function addCategoryUser(Request $request)
    {
        $input = $request->input();
        if (empty($input['categoryIdStr'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择品类');
        }
        $categoryIdArr = explode(',', $input['categoryIdStr']);
        $categoryArr = array();
        foreach ($categoryIdArr as $v1) {
            $categoryArr[] = Category::GetOne($v1);
        }
        foreach ($categoryArr as $k => $v) {
            $categoryUser = CategoryUser::GetOneCategoryUser(array('member_id' => $this->users['user_id'], 'category_id' => $v['id'], 'status' => CategoryUser::CATEGORY_USER_STATUS_ENABLE, 'is_delete' => CategoryUser::CATEGORY_USER_NOT_DELETE));
            if (empty($categoryUser)) {
                $addData = array(
                    'category_id' => $v['id'], //商户申请分类ID值
                    'ischeck' => CategoryUser::CATEGORY_USER_NOT_CHECK, //审核状态：1已审核 2待审核 3拒绝审核
                    'create_time' => time(), //创建时间
                    'status' => CategoryUser::CATEGORY_USER_STATUS_ENABLE, //状态：1启用 2禁用
                    'name' => $v['name'], //品类名称
                    'member_id' => $this->users['user_id'], //member_id
                    'check_refuse_reason' => null, //审核拒绝原因
                    'is_delete' => CategoryUser::CATEGORY_USER_NOT_DELETE, //是否删除  1是 2否
                    'update_time' => null, //更新时间
                );
                $res = CategoryUser::addData($addData);
            } else {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该品类已申请过');
            }
        }
        if ($res) {
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '添加成功请等待后台审核');
        } else {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '添加失败');
        }
    }

}
